<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <%@ include file="/common/common.jsp" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link href="${ctx}/static/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
    <link href="${ctx}/static/css/basic.css" rel="stylesheet" type="text/css">
    <link href="${ctx}/static/plugins/validationEngine/css/validationEngine.jquery.css" rel="stylesheet" type="text/css">
	<link rel="stylesheet" href="${ctx}/static/css/ztree/zTreeStyle.css" type="text/css">
    <style type="text/css">
        .btn-sm {
            border-width: 4px;
            font-size: 13px;
            padding: 4px 9px;
            line-height: 1.39;
        }

        .code_space {
            margin-left: -4px;
            margin-right: 14px;
        }

        .btn_key {
            background-color: #6fb3e0 !important;
            border-color: #6fb3e0;
        }

        .btn_key:hover {
            color: #fff;
            background-color: #39b3d7;
            border-color: #269abc;
        }

        .code_width {
            width: 36.5%;
        }

        @media only screen and (max-width: 767px ) {
            .code_width {
                width: 102%;
            }
        }

        .radios {
            margin-bottom: 20px;
            float: left;
            margin-left: 40px;
        }
    </style>
</head>
<body>
<div class="content">
	
    <form class="form-horizontal" id="updateRoleMenu" hidden>
    	<input id="roleId" name="roleId" value="${roleId}">
        <input id="menuIds" name="menuIds" value="">
    </form>
	    <br>
		<br>
		<br>
        <div id="rolemenu">
        	<div class="zTreeDemoBackground left">
				<ul id="treeDemo" class="ztree"></ul>
			</div>
        </div>
        
        <div class="form-group">
            <div class="col-sm-2">
                <button type="button" id="save" class="btn btn-primary">保存</button>
            </div>
            <div class="col-sm-2">
                <button type="button" id="cancle" class="btn btn-primary">取消</button>
            </div>
        </div>
</div>
<script src="${ctx}/static/plugins/jQuery/jQuery-2.1.4.min.js"></script>
<script src="${ctx}/static/bootstrap/js/bootstrap.min.js"></script>
<script src="${ctx}/static/plugins/layer/layer.js"></script>
<script src="${ctx}/static/plugins/validationEngine/jquery.validationEngine-zh_CN.js"></script>
<script src="${ctx}/static/plugins/validationEngine/jquery.validationEngine.js"></script>
<script src="${ctx}/static/js/common/base.js" type="text/javascript"></script>
<script src="${ctx}/static/js/common/page.js" type="text/javascript"></script>
<script type="text/javascript" src="${ctx}/static/js/ztree/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="${ctx}/static/js/ztree/jquery.ztree.core.min.js"></script>
<script type="text/javascript" src="${ctx}/static/js/ztree/jquery.ztree.excheck.min.js"></script>

<script type="text/javascript">

var setting = {
	check: {
		enable: true
	},
	data: {
		simpleData: {
			enable: true,
			
		}
	},
	callback : {
		onCheck: onCheck
	}
};
function onCheck(event, treeId, treeNode) {
	 var treeObj = $.fn.zTree.getZTreeObj("treeDemo"),
     nodes = treeObj.getCheckedNodes(true),
     v = new Array();
     for(var i=0;i<nodes.length;i++){
		v[i] = nodes[i].id;
     }
     $('#menuIds').val(v.join(","));
};
var zNodes;
$(document).ready(function(){
	//获取角色数据
  	$.ajax({
  		async:false,
  		type : "POST",
  		url: "${ctx}/api/role/getrolemenu", 
  		data:{roleId:$('#roleId').val()},
  		success 	: function(data) {
  			if(data.data){
  				zNodes=data.data;
  				var old = new Array();
  				var ii = 0;
  				if (zNodes){
  					for (var i=0; i<zNodes.length; i++) {
  						if (zNodes[i].checked){
	  						old[ii] = zNodes[i].id;
  							ii++;
  						}
  					}
  					$('#menuIds').val(old.join(","));
  				}
  			}else{
  				alert("加载菜单数据异常!");
  			}
  		}
  	});
	
	$.fn.zTree.init($("#treeDemo"), setting, zNodes);
	
	$('#save').click(function (){
		//提交数据
    	$.ajax({
    		async       : false,
    		type 		: "POST",
    	    url		    : "${ctx}/api/role/updateRoleMenu",
    		data 		: $("#updateRoleMenu").serialize(),
    		success 	: function( d ) {
    			if(d.code=="200"){
    				window.location.href="${ctx}/api/role/listPage";
    			} else {
    				alert(d.message);
    			}
    		}
    	});
	});
	
	$('#cancle').click(function (){
		window.location.href="${ctx}/api/role/listPage";
	});
	
});


</script>
</body>
</html>